package com.kangye.exam.service.serviceimpl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kangye.exam.bean.StudentPaper;
import com.kangye.exam.bean.TeacherPaper;
import com.kangye.exam.dao.TeacherPaperDao;
import com.kangye.exam.service.TeacherPaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Date;
import java.util.*;

@Service("teacherPaperService")
public class TeacherPaperServiceImpl implements TeacherPaperService{
    @Resource
    private TeacherPaperDao teacherPaperDao;

    @Override
    public PageInfo stupaperfind(Integer teacherId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Map<String,Object> map = new HashMap<>();
        map.put("teacherId",teacherId);
        List<StudentPaper> list = teacherPaperDao.findAll(map);
        PageInfo<StudentPaper> studentPaperPageInfo = new PageInfo<>(list);
        return studentPaperPageInfo;
    }

    @Override
    public String selectpath(Integer studentPaperId) {
        Map<String,Object> map = new HashMap<>();
        map.put("studentPaperId",studentPaperId);
        return teacherPaperDao.selectPath(map);
    }

    @Override
    public String jiexi(String stuentPath)throws IOException {
        FileReader fileReader= new FileReader(stuentPath);
        BufferedReader  bufferedReader= new BufferedReader(fileReader);
        return null;
    }

    @Override
    public Map<String,Object>  socre(String str,String studentName) {
        Integer score = 0;
        Map<String ,Object> map3 = new HashMap<>();//查找学生id
        map3.put("studentName",studentName);
        Integer studentId = Integer.parseInt(teacherPaperDao.selstudentId(map3));
        Map<String,Object> map = new HashMap<>();
        JSONObject jsonObject = JSON.parseObject(str);//类型转换为Json
        JSONArray array = jsonObject.getJSONObject("items").getJSONArray("data1");//将单选,多选,判断放进数组1
        JSONArray array1 = jsonObject.getJSONObject("items").getJSONArray("data2");//将简答,编程放进数组2
        Iterator<Object> iterator = array.iterator();
        List<Map> list = new ArrayList<>();
        while (iterator.hasNext()){
            Map<String,Object> map4 = new HashMap<>();//存放试卷信息
            Boolean flag = true;
            JSONObject object =(JSONObject) iterator.next();
            Integer itemId = object.getInteger("itemsid");
            map.put("itemId",itemId);
            Map<String,Object> map2 = new HashMap<>();//存放错题信息
            String stuanswer = object.getString("answer");
            String realanswer =teacherPaperDao.selansc(map).getAnswer();
            if(stuanswer!=null){//判断答案是否为空
                if (realanswer.equals(stuanswer)){
                    score=score+teacherPaperDao.selansc(map).getScore();
                    //将试题信息存入map文件并且转换成本地文件
                    map4.put("itemId",itemId);
                    map4.put("content",teacherPaperDao.selansc(map).getContent());
                    map4.put("options",teacherPaperDao.selansc(map).getOptions());
                    map4.put("stuanswer",stuanswer);
                    map4.put("realanswer",realanswer);
                    map4.put("analysis",teacherPaperDao.selansc(map).getAnalysis());
                    map4.put("flag",flag);
                }else {
                    //将试题信息存入map文件并且转换成本地文件
                    map4.put("itemId",itemId);
                    map4.put("content",teacherPaperDao.selansc(map).getContent());
                    map4.put("options",teacherPaperDao.selansc(map).getOptions());
                    map4.put("stuanswer",stuanswer);
                    map4.put("realanswer",realanswer);
                    map4.put("analysis",teacherPaperDao.selansc(map).getAnalysis());
                    map4.put("flag",flag);
                    //将错题存入错题本
                    map2.put("itemId",itemId);
                    map2.put("realanswer",realanswer);
                    map2.put("stuanswer",stuanswer);
                    map2.put("studentId",studentId);
                    Integer code = teacherPaperDao.insertwrong(map2);
                }
            }else{
                //将试题信息存入map文件并且转换成本地文件
                map4.put("itemId",itemId);
                map4.put("content",teacherPaperDao.selansc(map).getContent());
                map4.put("options",teacherPaperDao.selansc(map).getOptions());
                map4.put("stuanswer",stuanswer);
                map4.put("realanswer",realanswer);
                map4.put("analysis",teacherPaperDao.selansc(map).getAnalysis());
                map4.put("flag",flag);



                //将错题存入错题本
                map2.put("itemId",itemId);
                map2.put("answer",realanswer);
                map2.put("stuanswer",stuanswer);
                map2.put("studentId",studentId);
                Integer falg = teacherPaperDao.insertwrong(map2);
            }

            list.add(map4);
        }
        Map<String,Object>map1 = new HashMap<>();
        map1.put("socre",score);
        map1.put("data2",array1);
        map1.put("list",list);
        map1.put("studentId",studentId);
        map1.put("studentName",studentName);
        return map1;
    }



    @Override
    public Integer insertteacer(Integer studentPaperId, Integer score1, Integer socre2, Integer studentId, Integer paperId,String paperPath) {
        Map<String,Object> map = new HashMap<>();
        map.put("paperId",paperId);
        map.put("score",score1+socre2);
        map.put("studentId",studentId);
        map.put("correctingPath",paperPath);
        teacherPaperDao.insertteacher(map);
        return null;
    }

    @Override
    public PageInfo selscore(String paperName, String studentName, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Map<String,Object> map = new HashMap<>();
        map.put("pageName",paperName);
        map.put("studentName",studentName);
        List<TeacherPaper> list =teacherPaperDao.selscore(map);
        PageInfo<TeacherPaper> paperPageInfo = new PageInfo<>(list);
        return paperPageInfo;
    }
}
